package services.book;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import repositories.PublisherRepository;
import services.GenericService;
import books.Publisher;
import dtos.PublisherDTO;

/**
 */
public class PublisherService extends GenericService {

    private static final long serialVersionUID = 1L;

    private PublisherRepository publisherRepository;

    public void setPublisherRepository(final PublisherRepository publisherRepository) {
        this.publisherRepository = publisherRepository;
    }

    public PublisherRepository getPublisherRepository() {
        return this.publisherRepository;
    }

    @Transactional(readOnly = true)
    public List<Publisher> getAllPublishers() {
        return this.getPublisherRepository().findAll();
    }

    @Transactional
    public void addPublisher(final Publisher publisher) {
        this.getPublisherRepository().save(publisher);
    }

    @Transactional(readOnly = true)
    public List<Publisher> getPublishersByName(final String name) {
        return this.getPublisherRepository().findPublisherByName(name);
    }

    @Transactional
    public List<PublisherDTO> getAllPublishersDTO() {
        List<Publisher> publishers = this.getPublisherRepository().findAll();
        return this.getFactoryDto().toDtos(PublisherDTO.class, publishers);
    }

    @Transactional
    public Publisher findById(final long id) {
        return this.getPublisherRepository().findById(id);
    }
}
